package com.bee.beemanage.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bee.beemanage.api.entity.SysDict;
import com.bee.beemanage.api.entity.SysUser;
import com.bee.beemanage.api.entity.output.Dict;
import com.bee.beemanage.api.service.SysDictService;
import com.bee.beemanage.system.common.utils.context.CurrentUser;
import com.bee.beemanage.system.common.utils.pages.PageUtils;
import com.bee.beemanage.system.common.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import static com.bee.beemanage.system.common.utils.result.ResultTool.success;

@RestController
@RequestMapping("/bee-manage/sys-dict")
@Api(value ="字典表",tags = "字典表")
public class SysDictController {

    @Resource
    private SysDictService sysDictService;

    /**
     * 根据字典编码查询字典释义
     */
    @ApiOperation(value = "根据字典编码查询字典释意", notes = "根据字典编码查询字典释意")
    @GetMapping("/getByDictCode")
    public Result getByDictCode(Page<Dict> page, String dictCode, String label) {
        //传入的值如果是32位则为value
        String value = null;
        if(null!=label&&label.length()==32){
            value = label;
            label = null;
        }
        return success(sysDictService.pageGetDict(page,dictCode,label,value));
    }

    /**
     * 字典表分页查询
     * @param current     当前页
     * @param size        一页的条数
     * @param orderColumn 排序字段
     * @param orderAsc    升降序
     * @param sysDict     查询对象
     * @param startTime 创建起始时间
     * @param endTime 创建终止时间
     * @return 带分页信息的用户列表
     */
    @ApiOperation(value = "字典表分页查询", notes = "字典表分页查询")
    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('sys:dict:page')")
    public Result page(@RequestParam(defaultValue = "0") long current, @RequestParam(defaultValue = "10") long size,
                       String orderColumn, Boolean orderAsc, SysDict sysDict,
                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Result result;
        Page<SysDict> page = new Page<>();
        PageUtils.start(current, size, orderColumn, orderAsc, page);
        if (sysDict.getDeleted()) {
            result = success(sysDictService.recycleSearch(page, sysDict, startTime, endTime));
        } else {
            QueryWrapper<SysDict> query = Wrappers.query(sysDict);
            if (null != startTime && null != endTime) {
                query.between("create_time", startTime, endTime);
            }
            result = success(sysDictService.page(page, query));
        }
        return result;
    }
    /**
     * 根据id删除字典表
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id删除字典表", notes = "通过id删除字典表")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('sys:dict:removeById')")
    public Result removeById(@PathVariable String id) {
        return success(sysDictService.removeById(id));
    }
    /**
     * 根据id批量删除字典表
     * @param ids
     * @return
     */
    @ApiOperation(value = "通过id批量删除字典表", notes = "通过id批量删除字典表")
    @PostMapping("/batch")
    @PreAuthorize("hasAnyAuthority('sys:dict:removeByIds')")
    public Result removeByIds(@RequestBody String[] ids) {
        List<String> idsList = Arrays.asList(ids);
        return success(sysDictService.removeByIds(idsList));
    }

    /**
     * 根据id恢复逻辑删除的数据
     * @return 恢复是否成功
     */
    @ApiOperation(value = "根据id恢复逻辑删除的数据", notes = "根据id恢复逻辑删除的数据")
    @PostMapping("/recover")
    @PreAuthorize("hasAnyAuthority('sys:dict:recover')")
    public Result recover(@RequestBody String[] ids) {
        List<String> idsList = Arrays.asList(ids);
        return success(sysDictService.recover(idsList));
    }
    /**
     * 新增字典表
     * @param sysDict 字典对象
     * @return 是否成功
     */
    @ApiOperation(value = "新增字典表", notes = "新增字典表")
    @PostMapping
    @PreAuthorize("hasAnyAuthority('sys:dict:save')")
    public Result save(@RequestBody SysDict sysDict) {
        SysUser loginUser = CurrentUser.getLoginUser();
        sysDict.setCreateTime(LocalDateTime.now());
        sysDict.setCreateUser(loginUser.getId());
        sysDict.setCreateUser(loginUser.getId());
        return success(sysDictService.save(sysDict));
    }
    /**
     * 修改字典表
     * @param sysDict 字典对象
     * @return 是否成功
     */
    @ApiOperation(value = "修改字典表", notes = "修改字典表")
    @PostMapping("/edit")
    @PreAuthorize("hasAnyAuthority('sys:dict:edit')")
    public Result edit(@RequestBody SysDict sysDict) {
        SysUser loginUser = CurrentUser.getLoginUser();
        sysDict.setUpdateTime(LocalDateTime.now());
        sysDict.setUpdateUser(loginUser.getId());
        sysDict.setUpdateUser(loginUser.getId());
        return success(sysDictService.updateById(sysDict));
    }

    /**
     * 数据瘦身，物理删除所有逻辑删除的数据（真删）
     * @return 删除是否成功
     */
    @ApiOperation(value = "物理删除所有逻辑删除的数据", notes = "物理删除所有逻辑删除的数据")
    @DeleteMapping("/clear")
    @PreAuthorize("hasAnyAuthority('sys:dict:clear')")
    public Result clear() {
        return sysDictService.clear();
    }
}